package com.ali.juejin.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.ali.juejin.BR
import com.ali.juejin.R
import com.ali.juejin.base.BaseModelActivity
import com.ali.juejin.bean.home.followItem.Entrylist
import com.ali.juejin.bean.v1.Data
import com.ali.juejin.bean.v1.ItemInfo
import com.ali.juejin.ui.activity.ArticleDetailActivity
import kotlinx.android.synthetic.main.item_article_more.view.*

class ArticleMoreAdapter<K:ViewDataBinding,V: BaseModelActivity>(val context: V): RecyclerView.Adapter<ArticleMoreAdapter.ListHolder<K>>() {

    private var list = ArrayList<ItemInfo>()

    class ListHolder<T:ViewDataBinding>(val databind: T) : RecyclerView.ViewHolder(databind.root){
        fun getBinding():T{
            return databind
        }
    }

    fun fresh(params:ArrayList<ItemInfo>){
        list.clear()
        list.addAll(params)
//        LogUtils.w(list.size)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder<K> {
        return ListHolder(DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_article_more,parent,false))
    }

    override fun getItemCount(): Int {
        return list.size
    }


    override fun onBindViewHolder(holder: ListHolder<K>, position: Int) {
        val itemData = list[position]
        holder.itemView.article_item_more.setOnClickListener {
            ContextCompat.startActivity(
                context,
                Intent(context, ArticleDetailActivity::class.java).putExtra(
                    "id",
                    itemData.article_id
                ).putStringArrayListExtra("tag_ids",
                    itemData.article_info.tag_ids as java.util.ArrayList<String>?
                )
                .putExtra("user_id",itemData.author_user_info.user_id)
                ,
                null
            )
        }

        val binding = holder.getBinding()
        binding.setVariable(BR.relatedaEntry,itemData)
        binding.executePendingBindings()
    }

}